/*
Copyright 2009 http://code.google.com/p/toolkits/. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above
    copyright notice, this list of conditions and the following
    disclaimer in the documentation and/or other materials provided
    with the distribution.
  * Neither the name of http://code.google.com/p/toolkits/ nor the names of its
    contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.xfok.dict;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.*;
import java.util.LinkedHashMap; 

//import alex.reader.ebook.viewer.log;
import android.util.Log;
 


/*
class FuzzyMatch{
	String word;
	Location location;
}
*/
/*
class Expanations{
	public String word;
	public String expanation;
}
*/

public class StarDict {	
	/**/
	RandomAccessFile index;
	RandomAccessFile yaindex;
	DictZipFile dz;
	String dictname;
	public String last_error = "";
	int MaxDistance = 4;
	int MaxFuzzyWords = 10;
	
	/**
	 * 
	 */
	public StarDict() {
		//this("//sdcard/dict");
		this("//sdcard");
	}
	
	/**
	 * 
	 * @param dictname
	 */
	public StarDict(String dictname) {
		try {
			this.dictname = dictname;
			this.index = new RandomAccessFile(dictname+".idx", "r");
			this.dz = new DictZipFile(dictname+"_dict.dz");
			this.yaindex = new RandomAccessFile(dictname+".yaidx", "r");
			//this.dz.runtest();
		}
		catch(FileNotFoundException e) {
			last_error = e.toString();
			e.printStackTrace();
		}
		catch(Exception e) {
			last_error = e.toString();
			e.printStackTrace();
		}
	}
	
	public String getWord(int p, Location l) {
		if(l==null) {
			l = new Location();
		}
		String word = null;
		byte [] buffer = new byte[1024];
		int dataoffset = 0;
		int datasize = 0;
		int offset = 0; // the offset of the p-th word in this.index
		try {
			this.yaindex.seek(p*4);
			int size = this.yaindex.read(buffer, 0, 4);
			if (size!=4) {
				throw new Exception("Read Index Error");
			}
			for(int i=0;i<4;i++) {
				offset<<=8;
				offset|=buffer[i]&0xff;
			}
			this.index.seek(offset);
			size = this.index.read(buffer, 0, 1024);
			for(int i=0;i<size;i++) {
				if (buffer[i]==0) {
					word = new String(buffer, 0, i, "UTF8");
					dataoffset = 0;
					datasize = 0;
					for (int j=i+1;j<i+5;j++) {
						dataoffset<<=8;
						dataoffset|=buffer[j]&0xff;
					}
					for (int j=i+5;j<i+9;j++) {
						datasize<<=8;
						datasize|=buffer[j]&0xff;
					}
					break;
				}
			}
			//System.out.println(datasize);
			//buffer = new byte[datasize];
			//this.dz.seek(dataoffset);
			//this.dz.read(buffer, datasize);
			l.offset = dataoffset;
			l.size = datasize;
		}
		catch(Exception e) {
			last_error = e.toString();
			e.printStackTrace();
		}
		return word;
	}
	
	//qshsj added start 091222
	//find algorithm 
	public int myCompareWord(String str1,String str2){
		int minlen = 0;
		int result = 0;
		String lowStr1,lowStr2;
		int len1 = str1.length();
		int len2 = str2.length();
		if(len1 >= len2){
			minlen = len2;
		}
		else{
			minlen = len1;
		}
		Log.i("StarDict", "minlen:" + minlen);
		Log.i("StarDict", "str1:" + str1);
		Log.i("StarDict", "str2:" + str2);
		for(int i=0,j=0;i<minlen;) {

			int asc1 = str1.charAt(j);//73
			int asc2 = str2.charAt(i);//105
			
			Log.i("StarDict", "asc1:" + asc1);
			Log.i("StarDict", "asc2:" + asc2);

			if(str2.charAt(i) != str1.charAt(j)){
				if(str2.charAt(i) != str1.charAt(j)+32 && str1.charAt(j) != str2.charAt(i)+32){
					lowStr1 = str1.substring(j, j+1).toLowerCase();
					lowStr2 = str2.substring(i, i+1).toLowerCase();
					if(lowStr1.charAt(0) > lowStr2.charAt(0)){
						result = -1;//str1>str2(str1在str2�?
						//i++;
						//j++;
						break;
					}
					else{//str1<str2
						result = 1;//(str1在str2�?
						//i++;
						//j++;
						break;
					}
				}
				else if(str2.charAt(i) == str1.charAt(j)+32){
					if(i == minlen-1){
						if(len1 == len2){
							result = 0;
							break;
						}
						else if(len1 > len2){
							Log.i("StarDict", "prev111=" + result);
							result = -1;//(str1在str2�?
							//i++;
							//j++;
							break;
						}
						else{
							Log.i("StarDict", "next111=" + result);
							result = 1;
							//i++;
							//j++;
							break;
						}
					}
					else{
						result = 1;//(str1在str2�?
						i++;
						j++;
						continue;
					}
					//break;
				}
				else if(str1.charAt(j) == str2.charAt(i)+32){
					if(i == minlen-1){
						if(len1 == len2){
							result = 0;
							break;
						}
						else if(len1 > len2){
							Log.i("StarDict", "prev111=" + result);
							result = -1;//(str1在str2�?
							//i++;
							//j++;
							break;
						}
						else{
							Log.i("StarDict", "next111=" + result);
							result = 1;//(str1在str2�?
							//i++;
							//j++;
							break;
						}
					}
					else{
						result = -1;//(str1在str2�?
						i++;
						j++;
						continue;
					}
					//break;
				}
			}
			else if(str2.charAt(i) == str1.charAt(j)){
				Log.i("StarDict", "str2==str1" + 0);
				if(i == minlen-1){
					Log.i("StarDict", "i == minlen-1" + 0);
					if(len1 == len2){
						result = 0;
						break;
					}
					else if(len1 > len2){
						Log.i("StarDict", "prev111=" + result);
						result = -1;//(str1在str2�?
						//i++;
						//j++;
						break;
					}
					else{
						Log.i("StarDict", "next111=" + result);
						result = 1;//(str1在str2�?
						//i++;
						//j++;
						break;
					}
				}
				else{
					Log.i("StarDict", "i != minlen-1" + 0);
					result =0;
					i++;
					j++;
					continue;
				}
			}
			else
			{
				
			}
		}	
		return result;
	}
	//qshsj added end 091222
	
	//qshsj added start 091225
	public String checkString(String old){
		if (true) {
			return old;
		}
		String strnew = "";
		String tmp;
		String tmpprev;
		String tmpnext;
		int count = 0;
		Log.i("StarDict", "old=" + old);
		int len = old.length();
		for(int i=0;i<len;i++){
			char cr = old.charAt(i);
			tmp = old.substring(i,i+1);
			//if((cr>='A') && (cr<='Z') && (cr>='a') && (cr>='z')){
			if((tmp.compareToIgnoreCase("A")>=0) && (tmp.compareToIgnoreCase("Z")<=0) && (tmp.compareToIgnoreCase("a")>=0) && (tmp.compareToIgnoreCase("z")<=0)){
				strnew += old.substring(i, i+1);
				Log.i("StarDict", "strnew1***zimu=" + strnew);
			}
			else{
				Log.i("StarDict", "tmp=" + tmp);
				if((tmp.compareToIgnoreCase("-")!=0)&&(tmp.compareToIgnoreCase("'")!=0)){
					
				}
				else{
					if(i==0){
						strnew += old.substring(i, i+1);
						Log.i("StarDict", "strnew2***first(-')" + strnew);
						count++;
					}
					else if(i<len-1){
						tmpprev = old.substring(i-1,i);
						tmpnext = old.substring(i+1,i+2);
						Log.i("StarDict", "tmpprev=" + tmpprev);
						Log.i("StarDict", "tmpnext=" + tmpnext);
						if((tmpprev.compareToIgnoreCase("A")>=0) && (tmpprev.compareToIgnoreCase("Z")<=0) && (tmpprev.compareToIgnoreCase("a")>=0) && (tmpprev.compareToIgnoreCase("z")<=0) &&
								(tmpnext.compareToIgnoreCase("A")>=0) && (tmpnext.compareToIgnoreCase("Z")<=0) && (tmpnext.compareToIgnoreCase("a")>=0) && (tmpnext.compareToIgnoreCase("z")<=0)){
							if(count==0){
								strnew += old.substring(i, i+1);
								Log.i("StarDict", "strnew3***next=" + strnew);
							}
						}
					}
					else if(i==len-1){
						
					}
					else{
						
					}
				}
			}
		}
		Log.i("StarDict", "strnew result=" + strnew);
		return strnew;
	}
	//qshsj added end 091225
	
	/**
	 * 
	 * @param word
	 * @return the explanation of the word
	 */
	public int getSimple(String word,LinkedHashMap<String,Location> h) {
		int i = 0;
		int max = getWordNum();
		String w = "";
		int mid = 0;
		Location l = new Location();
		boolean found = false;
		int index = 0;
		//return this.dz.test()+this.dz.last_error;
		///*
		Log.i("StarDict", "change before=" + word);
		word = checkString(word);
		Log.i("StarDict", "change after=" + word);
		if(word==""){
			Log.i("StarDict", "word null=" + word);
			return -1;
		}
		
		while( i<=max ) {
			try{
				mid = (i + max)/2;
				w = getWord(mid, l);
				Log.i("StarDict", "w=" + w);
				
				if (w == null) {
					return 0;
				}
				
				if(myCompareWord(w,word)>0){//(w在word�?
					i = mid+1;
					Log.i("StarDict", "next=" + i);
				}
				else if(myCompareWord(w,word)<0) {//(w在word�?
					max = mid-1;
					Log.i("StarDict", "prev=" + i);
				}
				else{
					found=true;
					//Store Result in LinkedHashMap;
					Log.i("StarDict", "put word=" + word);
					h.put(word,l);
					index = i;
					break;
				}
				/*if (w.compareTo(word)>0) {
					max = mid-1;
				}
				else if(w.compareTo(word)<0) {
					i = mid+1;
				} 
				else {
					found=true;
					//Store Result in LinkedHashMap;
					h.put(word,l);
					break;
				}*/
			}catch(Exception err){
				last_error = err.toString();
				err.printStackTrace();
			}
			index = i;
		}
		if (!found){
			Log.i("StarDict", "not found w=" + w);
			h.put(w,l);
			//return -1;
		}
		return i;
	}
	
	//find by substring
	public int getSimpleSub(String word,LinkedHashMap<String,Location> h) {
		int i = 0;
		int max = getWordNum();
		String w = "";
		String strtmp=word;
		int mid = 0;
		Location l = new Location();
		boolean found = false;
		
		/*word = checkString(word);
		Log.i("StarDict", "sub word=" + word);
		if(word==""){
			Log.i("StarDict", "sub word null=" + word);
			return -1;
		}*/
		//return this.dz.test()+this.dz.last_error;
		///*
		while( i<=max ) {
			try{
				mid = (i + max)/2;
				w = getWord(mid, l);
				if (w == null) {
					return 0;
				}
				if ((w.length()>word.length())&& (word.compareTo(w.substring(0,word.length()))==0)){
					word = w;
				}
				
				if(myCompareWord(w,word)>0){//(w在word�?
					i = mid+1;
				}
				else if(myCompareWord(w,word)<0) {//(w在word�?
					max = mid-1;
				}
				else{
					found=true;
					//Store Result in LinkedHashMap;
					h.put(word,l);
					Log.i("StarDict", "put sub word=" + word);
					//int tmpMin = (mid - MaxFuzzyWords)>0?(mid-MaxFuzzyWords):0;
					int tmpMin = mid;
					int tmpMax = (mid+MaxFuzzyWords)<=getWordNum()?(mid+MaxFuzzyWords):getWordNum();
					Log.i("StarDict", "tmpMin*****=" + tmpMin);
					Log.i("StarDict", "tmpMax*****=" + tmpMax);
					while(tmpMin <= tmpMax){
						if (tmpMin == mid){tmpMin++;continue;}
						l = new Location();
						w = getWord(tmpMin, l);
						Log.i("StarDict", "put sub w*****111=" + w);
						Log.i("StarDict", "put sub l*****=" + l);
						h.put(w,l);
						/*if ((w.length()>strtmp.length())&&(strtmp.compareToIgnoreCase(w.substring(0,strtmp.length()))==0)){
							Log.i("StarDict", "put sub w*****=" + w);
							Log.i("StarDict", "put sub l*****=" + l);
							h.put(w,l);
						}*/
						tmpMin++;
					}
					break;
				}
				
				/*if (w.compareTo(word)>0) {
					max = mid-1;
				}
				else if(w.compareTo(word)<0) {
					i = mid+1;
				} 
				else {
					found=true;
					//Store Result in LinkedHashMap;
					h.put(word,l);
					//int tmpMin = (mid - MaxFuzzyWords)>0?(mid-MaxFuzzyWords):0;
					int tmpMin = mid;
					int tmpMax = (mid+MaxFuzzyWords)<=getWordNum()?(mid+MaxFuzzyWords):getWordNum();
					while(tmpMin <= tmpMax){
						if (tmpMin == mid){tmpMin++;continue;}
						l = new Location();
						w = getWord(tmpMin, l);
						if ((w.length()>strtmp.length())&&(strtmp.compareToIgnoreCase(w.substring(0,strtmp.length()))==0)){
							h.put(w,l);
						}
						tmpMin++;
					}
					break;
				}*/
			}catch(Exception err){
				last_error = err.toString();
				err.printStackTrace();
				return 0;
			}
		}
		if (!found){
			h.put(w,l);
			//int tmpMin = (mid - MaxFuzzyWords)>0?(mid-MaxFuzzyWords):0;
			int tmpMin = mid;
			int tmpMax = (mid+MaxFuzzyWords)<=getWordNum()?(mid+MaxFuzzyWords):getWordNum();
			while(tmpMin <= tmpMax){
				if (tmpMin == mid){tmpMin++;continue;}
				l = new Location();
				w = getWord(tmpMin, l);
				Log.i("StarDict", "put sub w*****111=" + w);
				Log.i("StarDict", "put sub l*****=" + l);
				h.put(w,l);
				/*if ((w.length()>strtmp.length())&&(strtmp.compareToIgnoreCase(w.substring(0,strtmp.length()))==0)){
					h.put(w,l);
				}*/
				tmpMin++;
			}
			//return -1;
		}
		return i;
	}
	
	//getFuzzy
	//Input word to find , LinkedHashMap for result storage
	//return number similar word found
	//this is for similarity search, for the distance set compare the
	//length of word search mask of matching list may differ a lots 
	//not suite for this version;
	@Deprecated
	public int getFuzzy(String word,LinkedHashMap<String,Location> h) {
		int i = 0;
		int max = getWordNum();
		String w = "";
		int fcount = 0;
		Location l = new Location();
		Similarity s = new Similarity();
		double sim;
		int ld;
		//return this.dz.test()+this.dz.last_error;
		///*
		while(i<=max) {
			try{
				w = getWord(i, l);
				i++;
				// Log.i("StarDict", "w=" + w);
				//no ABS
				if ((w.length()>word.length())&&(w.length()-word.length()>MaxDistance)){
					continue;
				}
				if ((w.length()<word.length())&&(word.length()-w.length()>MaxDistance)){
					continue;
				}
				
				if (fcount < MaxFuzzyWords){
					sim = s.sim(word,w);
					ld = s.ld(word,w);
					if ((ld <= MaxDistance)&&(sim >=0.5)){
						fcount++;
						h.put(w,l);
					}
				}else{
					//Reach Max match words,existing;
					break;
				}
			}catch(Exception err){
				last_error = err.toString();
				err.printStackTrace();
			}
		}
		return fcount;
	}
	
	//get explanation
	//Input Word=>Location Map
	//Return Word=>Explanation Map
	@Deprecated
	public LinkedHashMap<String,String> getExplanation(LinkedHashMap<String ,Location> wlmap){
		//result
		LinkedHashMap<String,String> wordexplain = new LinkedHashMap<String,String>();
		Location l = new Location();
		
		String exp = "";
		
		for (String ws : wlmap.keySet())
        {
			
    	    l  = wlmap.get(ws);

			Log.d("getExplanation",ws+l);
			
			byte [] buffer = new byte[l.size];
			this.dz.seek(l.offset);
			try {
				this.dz.read(buffer, l.size);
			}
			catch(Exception e) {
				last_error = e.toString();
				buffer = null;
				exp = e.toString();
			}
			
			try {
				if (buffer == null) {
					exp = "Error when reading data\n"+exp;
				}
				else {
					exp = new String(buffer, "UTF8");
				}
			}
			catch(Exception e) {
				last_error = e.toString();
				e.printStackTrace();
			}
			wordexplain.put(ws,exp);
			// return w+"\n"+exp;
			//return mid+"\n"+l.offset+exp+l.size;
		}
		return wordexplain;
	}
	
	//for testing
	@Deprecated
	public LinkedHashMap<String,String> getExplanationTest(LinkedHashMap<String ,Location> wlmap){
		//result
		LinkedHashMap<String,String> wordexplain = new LinkedHashMap<String,String>();
		Location l = new Location();
		
		String exp = "";
		
		for (String ws : wlmap.keySet())
        {
			
    	    
			wordexplain.put(ws,ws);
			// return w+"\n"+exp;
			//return mid+"\n"+l.offset+exp+l.size;
		}
		return wordexplain;
	}
	
	
	//get explanation for special word by it's location
	//used with getSearchMatchList(word,LinkedHashMap)
	public String getExplanation(Location l){
			//result
			String exp = "";
		
			
			byte [] buffer = new byte[l.size];
			this.dz.seek(l.offset);
			try {
				this.dz.read(buffer, l.size);
			}
			catch(Exception e) {
				last_error = e.toString();
				buffer = null;
				exp = e.toString();
			}
			
			try {
				if (buffer == null) {
					exp = "Error when reading data\n"+exp;
				}
				else {
					exp = new String(buffer, "UTF8");
				}
			}
			catch(Exception e) {
				last_error = e.toString();
				e.printStackTrace();
			}
		
		return exp;
	}
	
	//getexplanations
	//Main Entrance for UI
	//Problem: the getExpanation need iterate the dict.dz, that cause bad
	//performance
	@Deprecated
	public Map<String,String> getExplanations(String word){
			String origin=word;
			
			LinkedHashMap<String,Location> hashmap = new LinkedHashMap<String,Location>();	
			
			int intSimplePos = getSimple(word,hashmap);
			
			origin = getSearchedWordOrigin(word);
			
			if (origin.compareToIgnoreCase(word)==0){
				intSimplePos = getSimpleSub(word,hashmap);
			}else{
				intSimplePos = getSimple(origin,hashmap);
				intSimplePos = getSimpleSub(origin,hashmap);
			}
			//return getExplanation(hashmap);
			return getExplanationTest(hashmap);
   }
	
	
	//get search pattern matching words list first
	//then call getExplanation(Location l) for each word individually
	//for better user experience on UI
	public LinkedHashMap<String,Location> getSearchMatchList(String word){
		
		word = checkString(word);
		String origin=word;
		
		LinkedHashMap<String,Location> hashmap = new LinkedHashMap<String,Location>();		
		
		int intSimplePos = getSimple(word,hashmap);
		
		if(intSimplePos == -1){
			Log.i("StarDict", "chinese=" + intSimplePos);
			return hashmap;
		}
		
		//origin = getSearchedWordOrigin(word);
		Log.i("StarDict", "origin=" + origin);
		Log.i("StarDict", "word=" + word);
		
		if (origin.compareToIgnoreCase(word)==0){
			intSimplePos = getSimpleSub(word,hashmap);
		}
		else{
			intSimplePos = getSimple(origin,hashmap);
			intSimplePos = getSimpleSub(origin,hashmap);
		}
		
		/*for (Iterator iter = map.keySet().iterator(); iter.hasNext();) 
		{ Object key = iter.next(); 
		Object val = map.get(key); 
		System.out.println("key:"+key);
		System.out.println("value:"+val); 
		}*/
		return hashmap;
   }
	
	/**
	 * 
	 * @return
	 */
	public String getVersion() {
		try {
			BufferedReader br = new BufferedReader(new FileReader(this.dictname+".ifo"));
			String line = br.readLine();
			while(line != null) {
				String [] version = line.split("=");
				if (version.length == 2 && version[0].equals("version")) {
					return version[1];
				}
				line = br.readLine();
			}
		}
		catch(IOException e) {
			last_error = e.toString();
			e.printStackTrace();
		}
		return "UNKNOWN VERSION";
	}
	
	public int getWordNum() {
		try {
			BufferedReader br = new BufferedReader(new FileReader(this.dictname+".ifo"));
			String line = br.readLine();
			while(line != null) {
				String [] version = line.split("=");
				if (version.length == 2 && version[0].equals("wordcount")) {
					return Integer.parseInt(version[1]);
				}
				line = br.readLine();
			}
		}
		catch(IOException e) {
			last_error = e.toString();
			e.printStackTrace();
		}
		return 0;
	}


	//Judge the origin form
	//getSimilarity{
	
	//cut "ied" , add "y".
	//cut "ies" , add "y"

	//cut "ing"
	//cut "est"
	
	//cut "es"
	//cut "ed"
	//cut "er"
	//cut "ly"
	
	//cut one char "s" or "d"

	//}
	public String getSearchedWordOrigin(String old){
		String strnew=old;
		String tmp;
		
		if (old.length()>3){
			tmp = old.substring(old.length()-3);
			if ((tmp.compareToIgnoreCase("ied")==0)||(tmp.compareToIgnoreCase("ies")==0)){
				strnew = old.substring(0, old.length()-3)+"y";
			}
			else if ((tmp.compareToIgnoreCase("ing")==0)||(tmp.compareToIgnoreCase("est")==0)){
				strnew = old.substring(0, old.length()-3);
			}
			else{
				tmp = old.substring(old.length()-2);
				if ((tmp.compareToIgnoreCase("ed")==0)||(tmp.compareToIgnoreCase("es")==0)||(tmp.compareToIgnoreCase("er")==0)||(tmp.compareToIgnoreCase("ly")==0)||(tmp.compareToIgnoreCase("'s")==0)){
					strnew = old.substring(0, old.length()-2);
				}
				else{
					tmp = old.substring(old.length()-1);
					if ((tmp.compareToIgnoreCase("s")==0)||(tmp.compareToIgnoreCase("d")==0)){
						strnew = old.substring(0, old.length()-1);
					}
					else{
						tmp = old.substring(1);
						if((tmp.compareToIgnoreCase("-")!=0)||(tmp.compareToIgnoreCase("'")!=0)){
							
						}
					}
				}
			}
		}
		/*if (old.length()>2){
			tmp = old.substring(old.length()-2);
			if ((tmp.compareToIgnoreCase("ed")==0)||(tmp.compareToIgnoreCase("es")==0)||(tmp.compareToIgnoreCase("er")==0)||(tmp.compareToIgnoreCase("ly")==0)){
				strnew = old.substring(0, old.length()-2);
			}
		}
		if (old.length()>1){
			tmp = old.substring(old.length()-1);
			if ((tmp.compareToIgnoreCase("s")==0)||(tmp.compareToIgnoreCase("d")==0)){
				strnew = old.substring(0, old.length()-1);
			}
		}*/
		
		return strnew; 
	}
}
